home *** CD-ROM | disk | FTP | other *** search
- /* HyperCard XFCN to grab a ptr record and return with the value of the
- * ptr itself (the offset into the text file) ...
- *
- * called as getPtrRecord ( N, P)
- *
- * N = ptr record number of first line to display
- * P = ptr file refNum
- *
- * The result is returned as a nice printable zero-terminated string,
- * consisting simply of the (decimal) value of the ptr....
- *
- * If a mistake is detected, a null string is returned and the function
- * also emits a beep....
- *
- * Function is stored as XFCN number 600, named "getPtrRecord"....
- *
- * 871208 ^z
- */
-
- #include <MacTypes.h>
- #include <FileMgr.h>
- #include <OSUtil.h>
- #include <HyperXCmd.h>
- #include <proto.h>
-
-
- pascal void main (XCmdBlockPtr paramPtr);
- long getThePtr (long ptrNum, int refNum);
- int putNum (char *ans, long num);
- void complain (XCmdBlockPtr paramPtr);
-
-
- pascal void main (paramPtr)
- XCmdBlockPtr paramPtr;
- {
- int ptrFileNum;
- long ptrNum, ptr;
- int len;
- Handle answer;
-
- if (paramPtr->paramCount != 2)
- {
- complain (paramPtr);
- return;
- }
-
- ptrNum = atol (*(paramPtr->params[0]));
- ptrFileNum = atol (*(paramPtr->params[1]));
-
- if (ptrNum < 0 || ptrFileNum == 0)
- {
- complain (paramPtr);
- return;
- }
-
- answer = NewHandle (32);
- ptr = getThePtr (ptrNum, ptrFileNum);
- len = putNum (*answer, ptr);
- *(*answer + len) = '\0';
- paramPtr->returnValue = answer;
- return;
- }
-
-
- /* function to fetch the value of the nth ptr from file ptrFileNum ...
- * return illegal value (-1) for result if something goes wrong....
- */
-
- long getThePtr (n, ptrFileNum)
- long n;
- int ptrFileNum;
- {
- long bytes = sizeof (long), result;
-
- if (SetFPos (ptrFileNum, fsFromStart, n * sizeof (long)) != noErr ||
- FSRead (ptrFileNum, &bytes, &result) != noErr)
- return (-1);
-
- return (result);
- }
-
-
-
- /* function to beep and set the return string to null (= "")
- */
-
- void complain (paramPtr)
- XCmdBlockPtr paramPtr;
- {
- Handle answer;
-
- SysBeep (10);
- answer = NewHandle (1);
- **answer = '\0';
- paramPtr->returnValue = answer;
- return;
- }
-
-
-
- /* function to convert alphabetic string to a long integer ... from LSC
- * library.... simplified to avoid using isspace() & isdigit() .... */
-
- long atol (s)
- register char *s;
- {
- register char signflag = 0;
- register long r = 0;
-
- while ((*s == ' '))
- s++;
-
- if (*s == '-')
- {
- signflag = 1;
- s++;
- }
- else if (*s == '+')
- s++;
-
- while (*s >= '0' && *s <= '9')
- r = r * 10 + (*s++ - '0');
-
- return (signflag ? -r : r);
- }
-
-
- /* function to convert a number into a string and put it into the chosen
- * target place ... returns the number of characters stored ...
- * based on K&R p. 60 example of itoa()....
- */
-
- int putNum (ans, num)
- char *ans;
- long num;
- {
- int i, j, s, result;
-
- i = 0;
- s = 1;
- if (num < 0)
- {
- num = -num;
- s = -1;
- }
-
- do
- ans[i++] = num % 10 + '0';
- while ((num /= 10) > 0);
-
- if (s < 0)
- ans[i++] = '-';
- result = i;
-
- for (--i, j = 0; j < i; ++j, --i)
- {
- s = ans[i];
- ans[i] = ans[j];
- ans[j] = s;
- }
-
- return (result);
- }
-
-